package com.rongshu.sendmessage.util.addimage;

import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.rongshu.sendmessage.base.MyApplication;
import com.rongshu.sendmessage.util.httpUtil.HYContent;

import java.io.File;
import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class UpFileUtil {
    public static final int SAVE_SUCCESS = 0x1425;
    public static final int SAVE_FIAL = 0x125;

    public static void upFile(String url,Bitmap bitmap, File file, Handler handler){
        String userId=MyApplication.mSettings.getString(HYContent.UserId,"");
        OkHttpClient client = new OkHttpClient();
        // form 表单形式上传
        MultipartBody.Builder requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM);
        if (bitmap != null) {
            // MediaType.parse() 里面是上传的文件类型。
            //不压缩
//            Bitmap yaSuo = UIUtils.getYaSuo(bitmap);
            byte[] base64ImageStr = ReadImgToBinary2.getButyArray(bitmap);
            assert base64ImageStr != null;
            RequestBody body = RequestBody.create(MediaType.parse("image/*"),base64ImageStr);
            String filename = System.currentTimeMillis()+file.getName();
            // 参数分别为， 请求key ，文件名称 ， RequestBody
            if (new HYContent().SET_USER_HEAD_URL.equals(url)){//在这里上传头像
                requestBody.addFormDataPart("videofile", filename, body);
            }else{//这里是文件的名
                requestBody.addFormDataPart("invoiceImg", filename, body);
            }
            requestBody.addFormDataPart("userId",userId);
        }
//
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody.build()).build();
        // readTimeout("请求超时时间" , 时间单位);
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e){
               Log.e("UPDATA_ERROR",e.toString());
                Message msg=new Message();
                msg.what=SAVE_FIAL;
                handler.sendMessage(msg);
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    String responseStr = response.body().string();
                    Message msg=new Message();
                    msg.obj=responseStr;
                    msg.what=SAVE_SUCCESS;
                    handler.sendMessage(msg);
                }else{
                    Log.e("UPDATA_ERROR",response.body().toString());
                    Message msg=new Message();
                    msg.what=SAVE_FIAL;
                    handler.sendMessage(msg);
                }
                //这是回调内容
                Log.d("dongjing", "onResponse"+response.body().toString());
            }
        });
    }
}
